package com.youtellv1.mapper;

import com.youtellv1.entity.ChildSign;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface ChildSignMapper extends BaseMapper<ChildSign>{
    @Select("select * from tb_childsign")
    List<ChildSign> findAllChildSign();

    @Select("select * from tb_childsign where cid = #{cid}")
    ChildSign findByCid(@Param("cid") Integer cid);

    @Select("select count(*) from tb_childsign where cid = #{cid} and sdate = curdate()")
    Integer countByCidAndDate(@Param("cid") Integer cid, @Param("sdate") Date sdate);

    @Select("select sdate from tb_childsign where cid = #{cid}")
    List<Date> selectAllDatesByCid(@Param("cid") Integer cid);

    @Select("select sdate from tb_childsign where cid = #{cid} and sdate between #{start} and #{end}")
    List<Date> selectDatesByCidBetween(@Param("cid") Integer cid, @Param("start") Date start, @Param("end") Date end);

    @Select("select count(*) from tb_childsign where cid = #{cid} and sdate between #{start} and #{end}")
    int countByCidAndDateRange(@Param("cid") Integer cid,
                               @Param("start") String start,
                               @Param("end") String end);


    @Insert("insert into tb_childsign(sdate, cid) values (#{sdate}, #{cid})")
    void save(ChildSign childSign);

    @Insert("insert into tb_childsign (cid, sdate) values (#{cid}, curdate())")
    Integer insert(ChildSign sign);

    @Select("SELECT COUNT(*) FROM tb_childsign WHERE cid = #{cid} AND sdate = #{sdate}")
    int countByCidDate(@Param("cid") Integer cid, @Param("sdate") Date sdate);

    @Insert("INSERT INTO tb_childsign (cid, sdate) VALUES (#{cid}, #{sdate})")
    void insertChildSign(@Param("cid") Integer cid, @Param("sdate") Date sdate);


    @Update("<script>" +
            "update tb_childsign " +
            "<set>" +
            "    <if test='sdate != null'>sdate = #{sdate},</if>" +
            "    <if test='cid != null'>cid = #{cid}</if>" +
            "</set>" +
            "where sgid = #{sgid}" +
            "</script>")
    void update(ChildSign childSign);

    @Delete("delete from tb_childsign where sgid = #{sgid}")
    void delete(ChildSign childSign);
}
